<html>
<head><meta charset="utf-8"><title>Nomicon · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html">Nomicon</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="230849571"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230849571" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Gilcher <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230849571">(Mar 18 2021 at 11:49)</a>:</h4>
<p>Which team owns the nomicon currently? I have a number of questions, particularly around the "implementing Vec" chapter.</p>



<a name="230849716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230849716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Gilcher <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230849716">(Mar 18 2021 at 11:50)</a>:</h4>
<p>One of the current ones I have is that the first part of the tutorial says that we need to be variant over T: <a href="https://doc.rust-lang.org/nomicon/vec-layout.html">https://doc.rust-lang.org/nomicon/vec-layout.html</a></p>



<a name="230849802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230849802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Gilcher <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230849802">(Mar 18 2021 at 11:51)</a>:</h4>
<p>But looking at NonNull, it says that it is covariant over T, and: "Usually this won't be necessary; covariance is correct for most safe abstractions, such as Box, Rc, Arc, Vec, and LinkedList." <a href="https://doc.rust-lang.org/std/ptr/struct.NonNull.html">https://doc.rust-lang.org/std/ptr/struct.NonNull.html</a></p>



<a name="230849903"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230849903" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Gilcher <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230849903">(Mar 18 2021 at 11:52)</a>:</h4>
<p>Which one is correct? Because using NonNull would allow us to implement Unique easily on safe Rust.</p>



<a name="230850988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230850988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Gilcher <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230850988">(Mar 18 2021 at 12:02)</a>:</h4>
<p>I also can't replicate the behavior described in the chapter</p>
<blockquote>
<p>So a &amp;Vec&lt;&amp;'static str&gt; couldn't be used where an &amp;Vec&lt;&amp;'a str&gt; was expected.</p>
</blockquote>
<p>Is this something that went away in modern Rust editions?</p>



<a name="230852510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230852510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230852510">(Mar 18 2021 at 12:16)</a>:</h4>
<p>From what I'm reading in the first link, the issue is an artifact of the navie implementation of <code>(*mut T,usize,usize)</code>, as <code>*mut T</code> is invariant over <code>T</code>.  This sentance before the behaviour described supports this, stating the naive implementation is incorrect because of the (in)variance.</p>
<blockquote>
<p>And indeed this would compile. Unfortunately, it would be incorrect. First, the compiler will give us too strict variance</p>
</blockquote>
<p>NonNull may still be better to discuss directly using <code>*const T</code>, as it expresses the non-null invariant, and gives a null pointer optimization. It may also be a good idea to <em>not</em> suggest using Unique, which is an implementation-detail AFAIK (rather than just being unstable pending stablization).</p>



<a name="230853060"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230853060" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Gilcher <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230853060">(Mar 18 2021 at 12:21)</a>:</h4>
<p>Well, i gathered as much, but further down, the chapter gives 4 requirements, and one of that is "We are variant over T". But NonNull says it is covariant, but also that it is fitting for Vec. So, is "variant" really a requirement or just a behaviour of Unique?</p>



<a name="230853392"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230853392" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Gilcher <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230853392">(Mar 18 2021 at 12:24)</a>:</h4>
<p>So, yes, moving the chapter to NonNull would probably tighten it up a lot, but i'm struggling with the question whether I need deal with the variance issue or if that can be dropped along  the way.</p>



<a name="230853491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230853491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230853491">(Mar 18 2021 at 12:25)</a>:</h4>
<p>Ah.<br>
I wonder if that's outdated terminology. I would presume it should be <code>covariant</code>, but it seems to mean the same. (Rather than <code>variant</code> meaning <code>invariant</code>).</p>



<a name="230853630"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230853630" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Gilcher <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230853630">(Mar 18 2021 at 12:26)</a>:</h4>
<p>ah!</p>



<a name="230853668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230853668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Gilcher <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230853668">(Mar 18 2021 at 12:26)</a>:</h4>
<p>In this case, dropping down to <code>NonNull</code> from <code>Unique</code> would be just feasible.</p>



<a name="230853680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230853680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230853680">(Mar 18 2021 at 12:26)</a>:</h4>
<p>The definition of <code>Unique&lt;T&gt;</code> it provides is definately covariant, <code>*const T</code> is covariant (and even indicates that it is using <code>*const</code> "for variance"), and <code>PhantomData&lt;T&gt;</code> is variant, because it's just <code>T</code>.</p>



<a name="230853743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230853743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Gilcher <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230853743">(Mar 18 2021 at 12:27)</a>:</h4>
<p>Because the rest of the chapter is also weird around Unique, e.g. making a case for why <code>new</code> is okay to be safe, but that would break with very easy API extensions that rely on the uniquenes...</p>



<a name="230853755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230853755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Connor Horman <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230853755">(Mar 18 2021 at 12:27)</a>:</h4>
<p>Well, it would still need the other implications of <code>Unique</code> (such as being the Unique Owner of a <code>T</code>).</p>



<a name="230853883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230853883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Gilcher <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230853883">(Mar 18 2021 at 12:28)</a>:</h4>
<p>From the perspective of the goal of the chapter, this is an invariant of <code>Vec</code> though, and that completely encapsulates that behaviour.</p>



<a name="230856651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230856651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230856651">(Mar 18 2021 at 12:50)</a>:</h4>
<p>I think where the nomicon says "variant" it really means "covariant", yeah</p>



<a name="230871492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230871492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230871492">(Mar 18 2021 at 14:23)</a>:</h4>
<p><span class="user-mention" data-user-id="215333">@Florian Gilcher</span> the main distinction between <code>Unique</code> and <code>NonNull</code> is regarding <code>dropck</code> if one were to use, say</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">unsafe</span><span class="w"> </span><span class="k">impl</span><span class="o">&lt;</span><span class="cp">#[may_dangle]</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">Drop</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Wrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"></span>
</code></pre></div>
<p>In that case, if the only usage of <code>T</code> within <code>Wrapper&lt;T&gt;</code> definition is <code>NonNull&lt;T&gt;</code> (or <code>*const|mut T</code>), then that drop impl will be unsound (if it happens to drop instances of type <code>T</code>). On the other hand, if <code>PhantomData&lt;T&gt;</code> were to be used, (such as inside <code>Unique</code>'s definition), then such an impl would be correct. See <a href="https://users.rust-lang.org/t/phantomdata-and-dropck-confusion/26624/2">this lengthy post of mine about it</a>.</p>
<hr>
<p>Also, fwiw, I've always found:</p>
<blockquote>
<p>Usually this won't be necessary; covariance is correct for most safe abstractions, such as Box, Rc, Arc, Vec, and LinkedList</p>
</blockquote>
<p>to be incredibly <em>opaque</em> for something that has to do with safety.</p>
<p>For those really wanting a one-liner TL,DR, the key idea is: covariance is fine if you have immutable access to the <code>T</code>, or if you <em>uniquely own</em>  the <code>T</code>; otherwise covariance is likely to be unsound and you should conservatively be invariant.</p>
<ul>
<li>You can notice the enumeration lists cases of the latter, but for <code>Rc/Arc</code> potential multiple owners, but then we are back to immutability (which is broken if the pointee is wrapped in a shared mutability wrapper, which is why those are the exception that needs to be invariant).</li>
</ul>
<p>Since <code>Unique</code> is precisely the wrapper for unique <em>ownership</em> (it's sad that the own part is not in the name: using <code>Unique&lt;T&gt;</code> to mimic <code>&amp;'short-lived mut T</code> semantics is, for instance, wrong).</p>



<a name="230979558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Nomicon/near/230979558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Nomicon.html#230979558">(Mar 19 2021 at 05:47)</a>:</h4>
<p><span class="user-mention silent" data-user-id="215333">Florian Gilcher</span> <a href="#narrow/stream/213817-t-lang/topic/Nomicon/near/230849571">said</a>:</p>
<blockquote>
<p>Which team owns the nomicon currently? I have a number of questions, particularly around the "implementing Vec" chapter.</p>
</blockquote>
<p>That'd be the lang-team-docs team of which specific members are specifically for the nomicon. The #t-lang/doc channel is the "correct" channel for questions w.r.t. the nomicon.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>